function setNewInterval2(fromCalendarId, fromHourId, fromMinuteId, toCalendarId, toHourId, toMinuteId, intervalMinFromDate, intervalMaxFromDate, intervalMinToDate, intervalMaxToDate, intervalMinLength, intervalMaxLength, hfFromId, hfToId)
{
    var fromCalendar = CalendarPopup_FindCalendar(fromCalendarId);
    var fromCalendarDate = fromCalendar.GetDate();
    var fromDay = Number(fromCalendarDate.getDate());
    var fromMonth = fromCalendarDate.getMonth();
    var fromYear = fromCalendarDate.getYear();
    var toCalendar = CalendarPopup_FindCalendar(toCalendarId);
    var toCalendarDate = toCalendar.GetDate();
    var toDay = Number(toCalendarDate.getDate());
    var toMonth = toCalendarDate.getMonth();
    var toYear = toCalendarDate.getYear();
    
    if(fromYear < 200)
    {
        fromYear += 1900;
    }
    if(toYear < 200)
    {
        toYear += 1900;
    }
    
    var selFromHour = document.getElementById(fromHourId);
    var selFromMinute = document.getElementById(fromMinuteId);
    var selToHour = document.getElementById(toHourId);
    var selToMinute = document.getElementById(toMinuteId);
    
    var selFromHourSelVal = selFromHour.options[selFromHour.selectedIndex].value;
    var selFromMinuteSelVal = selFromMinute.options[selFromMinute.selectedIndex].value;
    var selToHourSelVal = selToHour.options[selToHour.selectedIndex].value;
    var selToMinuteSelVal = selToMinute.options[selToMinute.selectedIndex].value;
    
    var tempFromDate = new Date(fromYear, fromMonth, fromDay, selFromHourSelVal, selFromMinuteSelVal);
    var tempFromDateMin = new Date(fromYear, fromMonth, fromDay, selFromHourSelVal, selFromMinuteSelVal);
    var tempFromDateMax = new Date(fromYear, fromMonth, fromDay, selFromHourSelVal, selFromMinuteSelVal);
    var tempToDate = new Date(toYear, toMonth, toDay, selToHourSelVal, selToMinuteSelVal);
    
    // Calendar section
    var fromCalSelDate = new Date(fromYear, fromMonth, fromDay);
    var toCalSelDate = new Date(toYear, toMonth, toDay);
    if(intervalMaxFromDate < tempFromDate)
    {
        tempFromDate = new Date(intervalMaxFromDate.getFullYear(), intervalMaxFromDate.getMonth(), intervalMaxFromDate.getDate(), intervalMaxFromDate.getHours(), intervalMaxFromDate.getMinutes());
        tempFromDateMin = new Date(intervalMaxFromDate.getFullYear(), intervalMaxFromDate.getMonth(), intervalMaxFromDate.getDate(), intervalMaxFromDate.getHours(), intervalMaxFromDate.getMinutes());
        tempFromDateMax = new Date(intervalMaxFromDate.getFullYear(), intervalMaxFromDate.getMonth(), intervalMaxFromDate.getDate(), intervalMaxFromDate.getHours(), intervalMaxFromDate.getMinutes());
        fromCalSelDate = new Date(tempFromDate.getFullYear(), tempFromDate.getMonth(), tempFromDate.getDate());
    }
    if(intervalMinFromDate > tempFromDate)
    {
        tempFromDate = new Date(intervalMinFromDate.getFullYear(), intervalMinFromDate.getMonth(), intervalMinFromDate.getDate(), intervalMinFromDate.getHours(), intervalMinFromDate.getMinutes());
        tempFromDateMin = new Date(intervalMinFromDate.getFullYear(), intervalMinFromDate.getMonth(), intervalMinFromDate.getDate(), intervalMinFromDate.getHours(), intervalMinFromDate.getMinutes());
        tempFromDateMax = new Date(intervalMinFromDate.getFullYear(), intervalMinFromDate.getMonth(), intervalMinFromDate.getDate(), intervalMinFromDate.getHours(), intervalMinFromDate.getMinutes());
        fromCalSelDate = new Date(tempFromDate.getFullYear(), tempFromDate.getMonth(), tempFromDate.getDate());
    }
    
    var tempMinutes = tempFromDateMin.getMinutes();
    tempMinutes += intervalMinLength;
    tempFromDateMin.setMinutes(tempMinutes);
    tempMinutes = tempFromDateMax.getMinutes();
    tempMinutes += intervalMaxLength;
    tempFromDateMax.setMinutes(tempMinutes);
    
    if(tempToDate < tempFromDateMin)
    {
        toCalSelDate = new Date(tempFromDateMin.getFullYear(), tempFromDateMin.getMonth(), tempFromDateMin.getDate());
        tempToDate = new Date(tempFromDateMin.getFullYear(), tempFromDateMin.getMonth(), tempFromDateMin.getDate(), tempFromDateMin.getHours(), tempFromDateMin.getMinutes());
    }
    if(tempToDate > tempFromDateMax)
    {
        toCalSelDate = new Date(tempFromDateMax.getFullYear(), tempFromDateMax.getMonth(), tempFromDateMax.getDate());
        tempToDate = new Date(tempFromDateMax.getFullYear(), tempFromDateMax.getMonth(), tempFromDateMax.getDate(), tempFromDateMax.getHours(), tempFromDateMax.getMinutes());
    }
    if(tempFromDateMin > intervalMinToDate)
    {
        intervalMinToDate = new Date(tempFromDateMin.getFullYear(), tempFromDateMin.getMonth(), tempFromDateMin.getDate(), tempFromDateMin.getHours(), tempFromDateMin.getMinutes());
    }
    if(tempFromDateMax < intervalMaxToDate)
    {
        intervalMaxToDate = new Date(tempFromDateMax.getFullYear(), tempFromDateMax.getMonth(), tempFromDateMax.getDate(), tempFromDateMax.getHours(), tempFromDateMax.getMinutes());
    }
    toCalendar.LowerBoundDate = new Date(intervalMinToDate.getFullYear(), intervalMinToDate.getMonth(), intervalMinToDate.getDate());
    toCalendar.UpperBoundDate = new Date(intervalMaxToDate.getFullYear(), intervalMaxToDate.getMonth(), intervalMaxToDate.getDate());

    fromCalendar.UpperBoundDate = new Date(intervalMaxFromDate.getFullYear(), intervalMaxFromDate.getMonth(), intervalMaxFromDate.getDate());
    fromCalendar.LowerBoundDate = new Date(intervalMinFromDate.getFullYear(), intervalMinFromDate.getMonth(), intervalMinFromDate.getDate());
    
    if(fromCalSelDate.getFullYear() < 201)
    {
        fromCalSelDate.setFullYear(Number(fromCalSelDate.getFullYear()) + 1900);
    }
    if(toCalSelDate.getFullYear() < 201)
    {
        toCalSelDate.setFullYear(Number(toCalSelDate.getFullYear()) + 1900);
    }
    if(fromCalendar.UpperBoundDate.getFullYear() < 201)
    {
        fromCalendar.UpperBoundDate.setFullYear(Number(fromCalendar.UpperBoundDate.getFullYear()) + 1900);
    }
    if(fromCalendar.LowerBoundDate.getFullYear() < 201)
    {
        fromCalendar.LowerBoundDate.setFullYear(Number(fromCalendar.LowerBoundDate.getFullYear()) + 1900);
    } 
    if(toCalendar.UpperBoundDate.getFullYear() < 201)
    {
        toCalendar.UpperBoundDate.setFullYear(Number(toCalendar.UpperBoundDate.getFullYear()) + 1900);
    }
    if(toCalendar.LowerBoundDate.getFullYear() < 201)
    {
        toCalendar.LowerBoundDate.setFullYear(Number(toCalendar.LowerBoundDate.getFullYear()) + 1900);
    } 
    
    fromCalendar.SelectDate(fromCalSelDate);
    toCalendar.SelectDate(toCalSelDate);
    // End Calendar section
    
    if(!selFromHourSelVal && selFromHour.length > 0)
    {
        selFromHourSelVal = selFromHour.options[0].value;
    }
    if(!selFromMinuteSelVal && selFromMinute.length > 0)
    {
        selFromMinuteSelVal = selFromMinute.options[0].value;
    }
    
    if(!selToHourSelVal && selToHour.length > 0)
    {
        selToHourSelVal = selToHour.options[selToHour.length - 1].value;
    }
    if(!selToMinuteSelVal && selToMinute.length > 0)
    {
        selToMinuteSelVal = selToMinute.options[selToMinute.length - 1].value;
    }
    // From Hour section
    var countHour = selFromHour.length;
    
    for(var i = countHour - 1; i >= 0; i--)
    {
        selFromHour.remove(i);
    }
    
    var loopFrom = 0;
    var loopTo = 23;
    if(fromCalSelDate.getFullYear() == intervalMinFromDate.getFullYear() && fromCalSelDate.getMonth() == intervalMinFromDate.getMonth() && fromCalSelDate.getDate() == intervalMinFromDate.getDate())
    {
        loopFrom = intervalMinFromDate.getHours();
    }
    if(fromCalSelDate.getFullYear() == intervalMaxFromDate.getFullYear() && fromCalSelDate.getMonth() == intervalMaxFromDate.getMonth() && fromCalSelDate.getDate() == intervalMaxFromDate.getDate())
    {
        loopTo = intervalMaxFromDate.getHours();
    }
    
    for(var i = Number(loopFrom); i <= Number(loopTo); i++)
    {
        var h = (Math.floor(i / 12) == 0 ? i : (i % 12));
	    if (h == 0)
	    {
		    h = 12;
	    }
	    if (Math.floor(i / 12) == 0)
	    {
		    insertOption(i,h + ' AM',selFromHour.id);
	    }
        else
        {
            insertOption(i,h + ' PM',selFromHour.id);
        }
    }
    
    selFromHour.selectedIndex = 0;
    for(var i = 0; i < selFromHour.length; i++)
    {
        if(selFromHour.options[i].value == selFromHourSelVal)
        {
            selFromHour.selectedIndex = i;
            break;
        }
        else if(Number(selFromHour.options[i].value) == Number(tempFromDate.getHours()))
        {
            selFromHour.selectedIndex = i;
        }
    }
    selFromHourSelVal = selFromHour.options[selFromHour.selectedIndex].value;
    // End From Hour section
    
    // From Minute section
    var countMinute = selFromMinute.length;
    
    for(var i = countMinute - 1; i >= 0; i--)
    {
        selFromMinute.remove(i);
    }
    
    loopFrom = 0;
    loopTo = 45;
    
    if(fromCalSelDate.getFullYear() == intervalMinFromDate.getFullYear() && fromCalSelDate.getMonth() == intervalMinFromDate.getMonth() && fromCalSelDate.getDate() == intervalMinFromDate.getDate() && Number(selFromHourSelVal) == intervalMinFromDate.getHours())
    {
        loopFrom = intervalMinFromDate.getMinutes();
    }
    if(fromCalSelDate.getFullYear() == intervalMaxFromDate.getFullYear() && fromCalSelDate.getMonth() == intervalMaxFromDate.getMonth() && fromCalSelDate.getDate() == intervalMaxFromDate.getDate() && Number(selFromHourSelVal) == intervalMaxFromDate.getHours())
    {
        loopTo = intervalMaxFromDate.getMinutes();
    }
    
    for(var i = Number(loopFrom); i <= Number(loopTo); i += 15)
    {
        var h;
        if(i == 0)
        {
            h = '00';
        }
        else
        {
            h = i;
        }
        insertOption(i, h, selFromMinute.id);
    }
    
    selFromMinute.selectedIndex = 0;
    for(var i = 0; i < selFromMinute.length; i++)
    {
        if(selFromMinute.options[i].value == selFromMinuteSelVal)
        {
            selFromMinute.selectedIndex = i;
            break;
        }
    }
    selFromMinuteSelVal = selFromMinute.options[selFromMinute.selectedIndex].value;
    // End From Minute section
    
    // To Hour section
    countHour = selToHour.length;
    
    for(var i = Number(countHour - 1); i >= 0; i--)
    {
        selToHour.remove(i);
    }
    
    loopFrom = 0;
    loopTo = 23;
    if(toCalSelDate.getFullYear() == intervalMinToDate.getFullYear() && toCalSelDate.getMonth() == intervalMinToDate.getMonth() && toCalSelDate.getDate() == intervalMinToDate.getDate())
    {
        loopFrom = intervalMinToDate.getHours();
    }
    if(toCalSelDate.getFullYear() == intervalMaxToDate.getFullYear() && toCalSelDate.getMonth() == intervalMaxToDate.getMonth() && toCalSelDate.getDate() == intervalMaxToDate.getDate())
    {
        loopTo = intervalMaxToDate.getHours();
    }
    if(toCalSelDate.getFullYear() == tempFromDateMin.getFullYear() && toCalSelDate.getMonth() == tempFromDateMin.getMonth() && toCalSelDate.getDate() == tempFromDateMin.getDate() && loopFrom < tempFromDateMin.getHours())
    {
        loopFrom = tempFromDateMin.getHours();
    }
    if(toCalSelDate.getFullYear() == tempFromDateMax.getFullYear() && toCalSelDate.getMonth() == tempFromDateMax.getMonth() && toCalSelDate.getDate() == tempFromDateMax.getDate() && loopTo > tempFromDateMax.getHours())
    {
        loopTo = tempFromDateMax.getHours();
    }
    
    if(fromCalSelDate.getFullYear() == toCalSelDate.getFullYear() && fromCalSelDate.getMonth() == toCalSelDate.getMonth() && fromCalSelDate.getDate() == toCalSelDate.getDate())
    {
        if(tempFromDateMin.getHours() > loopFrom)
        {
            loopFrom = tempFromDateMin.getHours();
        }
    }
    for(var i = Number(loopFrom); i <= Number(loopTo); i++)
    {
        var h = (Math.floor(i / 12) == 0 ? i : (i % 12));
	    if (h == 0)
	    {
		    h = 12;
	    }
	    if (Math.floor(i / 12) == 0)
	    {
		    insertOption(i,h + ' AM',selToHour.id);
	    }
        else
        {
            insertOption(i,h + ' PM',selToHour.id);
        }
    }
    
    selToHour.selectedIndex = 0;
    for(var i = 0; i < selToHour.length; i++)
    {
        if(selToHour.options[i].value == selToHourSelVal)
        {
            selToHour.selectedIndex = i;
            break;
        }
    }
    selToHourSelVal = selToHour.options[selToHour.selectedIndex].value;
    // End To Hour section
    
    // To Minute section
    countMinute = selToMinute.length;
    
    for(var i = Number(countMinute - 1); i >= 0; i--)
    {
        selToMinute.remove(i);
    }
    
    loopFrom = 0;
    loopTo = 45;
    
    if(toCalSelDate.getFullYear() == intervalMinToDate.getFullYear() && toCalSelDate.getMonth() == intervalMinToDate.getMonth() && toCalSelDate.getDate() == intervalMinToDate.getDate() && Number(selToHourSelVal) == intervalMinToDate.getHours())
    {
        loopFrom = intervalMinToDate.getMinutes();
    }
    if(toCalSelDate.getFullYear() == intervalMaxToDate.getFullYear() && toCalSelDate.getMonth() == intervalMaxToDate.getMonth() && toCalSelDate.getDate() == intervalMaxToDate.getDate() && Number(selToHourSelVal) == intervalMaxToDate.getHours())
    {
        loopTo = intervalMaxToDate.getMinutes();
    }
    if(toCalSelDate.getFullYear() == tempFromDateMin.getFullYear() && toCalSelDate.getMonth() == tempFromDateMin.getMonth() && toCalSelDate.getDate() == tempFromDateMin.getDate() && Number(selToHourSelVal) == tempFromDateMin.getHours() && loopFrom < tempFromDateMin.getMinutes())
    {
        loopTo = tempFromDateMin.getMinutes();
    }
    if(toCalSelDate.getFullYear() == tempFromDateMax.getFullYear() && toCalSelDate.getMonth() == tempFromDateMax.getMonth() && toCalSelDate.getDate() == tempFromDateMax.getDate() && Number(selToHourSelVal) == tempFromDateMax.getHours() && loopFrom < tempFromDateMax.getMinutes())
    {
        loopTo = tempFromDateMax.getMinutes();
    }
    if(fromCalSelDate.getFullYear() == toCalSelDate.getFullYear() && fromCalSelDate.getMonth() == toCalSelDate.getMonth() && fromCalSelDate.getDate() == toCalSelDate.getDate() && selFromHourSelVal == selToHourSelVal)
    {
        if(tempFromDateMin.getMinutes() > loopFrom)
        {
            loopFrom = tempFromDateMin.getMinutes();
        }
    }
    
    for(var i = Number(loopFrom); i <= Number(loopTo); i += 15)
    {
        var h;
        if(i == 0)
        {
            h = '00';
        }
        else
        {
            h = i;
        }
        insertOption(i, h, selToMinute.id);
    }
    
    selToMinute.selectedIndex = 0;
    for(var i = 0; i < selToMinute.length; i++)
    {
        if(selToMinute.options[i].value == selToMinuteSelVal)
        {
            selToMinute.selectedIndex = i;
            break;
        }
    }
    selToMinuteSelVal = selToMinute.options[selToMinute.selectedIndex].value;
    // End To Minute section
    
    // HiddenField section
    setDateTimeSelectorHiddenField(hfFromId, fromCalendarId, fromHourId, fromMinuteId);
    setDateTimeSelectorHiddenField(hfToId, toCalendarId, toHourId, toMinuteId);
//    var hfFrom = document.getElementById(hfFromId);
//    var hfTo = document.getElementById(hfToId);
//    fromCalendarDate = fromCalendar.GetDate();
//    toCalendarDate = toCalendar.GetDate();
//    
//    // fromYear fromMonth fromDay toYear toMonth toDay selFromHourSelVal selFromMinuteSelVal selToHourSelVal selToMinuteSelVal
//    
//    fromDay = Number(fromCalendarDate.getDate());
//    fromMonth = fromCalendarDate.getMonth();
//    fromYear = fromCalendarDate.getYear();
//    if(fromYear < 200)
//    {
//        fromYear += 1900;
//    }
//    
//    toDay = Number(toCalendarDate.getDate());
//    toMonth = toCalendarDate.getMonth();
//    toYear = toCalendarDate.getYear();
//    if(toYear < 200)
//    {
//        toYear += 1900;
//    }
//    
//    hfFrom.value = Number(selFromHourSelVal) + '/' + Number(selFromMinuteSelVal) + '/' + Number(fromDay) + '/' + Number(Number(fromMonth) + 1) + '/' + Number(fromYear);
//    hfTo.value = Number(selToHourSelVal) + '/' + Number(selToMinuteSelVal) + '/' + Number(toDay) + '/' + Number((toMonth) + 1) + '/' + Number(toYear);
    
    // End HiddenField section
}

function setDateTimeSelectorHiddenField(hiddenFieldId, calendarId, ddlHourId, ddlMinuteId)
{
    var calendar = CalendarPopup_FindCalendar(calendarId);
    var calendarDate = calendar.GetDate();
    var day = Number(calendarDate.getDate());
    var month = calendarDate.getMonth();
    var year = calendarDate.getYear();
    if (year < 200)
    {
        year += 1900;
    }
    var hour = document.getElementById(ddlHourId).options[document.getElementById(ddlHourId).selectedIndex].value;
    var minute = document.getElementById(ddlMinuteId).options[document.getElementById(ddlMinuteId).selectedIndex].value;
    
    var hf = document.getElementById(hiddenFieldId);
    hf.value = Number(hour) + '/' + Number(minute) + '/' + Number(day) + '/' + Number(Number(month) + 1) + '/' + Number(year);
}

function setNewToMinute(toCalendarId, toMinuteId, toHourId, hfToId)
{
    var toCalendar = CalendarPopup_FindCalendar(toCalendarId);
    var toCalendarDate = toCalendar.GetDate();
    var toDay = Number(toCalendarDate.getDate());
    var toMonth = toCalendarDate.getMonth();
    var toYear = toCalendarDate.getYear();
    
    if(toYear < 200)
    {
        toYear += 1900;
    }
    
    var selToHour = document.getElementById(toHourId);
    var selToMinute = document.getElementById(toMinuteId);
    var selToHourSelVal = selToHour.options[selToHour.selectedIndex].value;
    var selToMinuteSelVal = selToMinute.options[selToMinute.selectedIndex].value;
    var hfieldTo = document.getElementById(hfToId);
    
    hfieldTo.value = Number(selToHourSelVal) + '/' + Number(selToMinuteSelVal) + '/' + Number(toDay) + '/' + Number((toMonth) + 1) + '/' + Number(toYear);
}

function insertOption(value, text, selectId)
{
    var opt = document.createElement('option');
    opt.text = text;
    opt.value = value;
    var select = document.getElementById(selectId);
    try
    {
        select.add(opt,null); // standards compliant
    }
    catch(ex)
    {
        select.add(opt); // IE only
    }
}

function bindDDLDay(calendarId, ddlDaysId)
{
    var calendar = CalendarPopup_FindCalendar(calendarId);
    var calendarLowerBoudDate = calendar.LowerBoundDate;
    var calendarUpperBoudDate = calendar.UpperBoundDate;
    var minDayOfWeek = Number(calendarLowerBoudDate.getDay());
    var maxDayOfWeek = Number(calendarUpperBoudDate.getDay());
    var selectedDayOfWeek = Number(calendar.GetDate().getDay());
    
    var ddlDays = document.getElementById(ddlDaysId);
    
    if(ddlDays == null)
    {
        return;
    }
//    var selectedValue = Number(ddlDays.options[ddlDays.selectedIndex].value);
    
    var count = ddlDays.length;
    
    for(var i = count - 1; i >= 0; i--)
    {
        ddlDays.remove(i);
    }
    
    if(minDayOfWeek <= maxDayOfWeek)
    {
        var j = 0;
        for(var i = minDayOfWeek; i <= maxDayOfWeek; i++, j++)
        {
            insertOption(i, getDayName(i), ddlDays.id);
            if(i == selectedDayOfWeek)
            {
                ddlDays.selectedIndex = j;
            }
        }
    }
    else
    {
        var j = 0;
        for(var i = minDayOfWeek; i < 7; i++)
        {
            insertOption(i, getDayName(i), ddlDays.id);
            if(i == selectedDayOfWeek)
            {
                ddlDays.selectedIndex = j;
            }
            j += 1;
        }
        
        for(var i = 0; i <= maxDayOfWeek; i++)
        {
            insertOption(Number(i) + 7, getDayName(i), ddlDays.id);
            if(i == selectedDayOfWeek)
            {
                ddlDays.selectedIndex = j;
            }
            j += 1;
        }
    }
}

function setCalendar(calendarId, ddlDaysId)
{
    var calendar = CalendarPopup_FindCalendar(calendarId);
    var calendarDate = calendar.GetDate();
    var day = Number(calendarDate.getDate());
    var month = calendarDate.getMonth();
    var year = calendarDate.getYear();
    var calendarLowerBoudDate = calendar.LowerBoundDate;
    var calendarUpperBoudDate = calendar.UpperBoundDate;
    var minDayOfWeek = Number(calendarLowerBoudDate.getDay());
    var maxDayOfWeek = Number(calendarUpperBoudDate.getDay());
    
    if(year < 200)
    {
        year += 1900;
    }
    
    var ddlDays = document.getElementById(ddlDaysId);
    var ddlDaysSelVal = Number(ddlDays.options[ddlDays.selectedIndex].value);
    var selectedDayOfWeek = Number(calendarDate.getDay());
    
    if(selectedDayOfWeek < minDayOfWeek)
    {
        selectedDayOfWeek += 7;
    }
    
    day = day + ddlDaysSelVal - selectedDayOfWeek;
    var calSelDate = new Date(year, month, day);
    calendar.SelectDate(calSelDate);
}

function bindDdlHours(calendarId, ddlHoursId, minDayMinHour, maxDayMaxHour)
{
    var calendar = CalendarPopup_FindCalendar(calendarId);
    var calendarDate = calendar.GetDate();
    var calendarLowerBoundDate = calendar.LowerBoundDate;
    var calendarUpperBoundDate = calendar.UpperBoundDate;
    
    var ddlHours = document.getElementById(ddlHoursId);
    
    var minHour = 0;
    var maxHour = 23;
    
    if (calendarDate.getFullYear() == calendarLowerBoundDate.getFullYear() && 
        calendarDate.getMonth() == calendarLowerBoundDate.getMonth() &&
        calendarDate.getDate() == calendarLowerBoundDate.getDate())
    {
        minHour = Number(minDayMinHour);
    }
    if (calendarDate.getFullYear() == calendarUpperBoundDate.getFullYear() && 
        calendarDate.getMonth() == calendarUpperBoundDate.getMonth() &&
        calendarDate.getDate() == calendarUpperBoundDate.getDate())
    {
        maxHour = Number(maxDayMaxHour);
    }
    
    var selectedValue = null;
    if(ddlHours.length > 0)
    { 
        selectedValue = ddlHours.options[ddlHours.selectedIndex].value;
    }
    
    var count = ddlHours.length;
    
    for(var i = count - 1; i >= 0; i--)
    {
        ddlHours.remove(i);
    }
    
    for(var i = Number(minHour); i <= Number(maxHour); i++)
    {
        var h = (Math.floor(i / 12) == 0 ? i : (i % 12));
	    if (h == 0)
	    {
		    h = 12;
	    }
	    if (Math.floor(i / 12) == 0)
	    {
		    insertOption(i,h + ' AM',ddlHours.id);
	    }
        else
        {
            insertOption(i,h + ' PM',ddlHours.id);
        }
    }
    
    ddlHours.selectedIndex = 0;
    if(selectedValue != null)
    {
        for(var i = 0; i < ddlHours.length; i++)
        {
            if(ddlHours.options[i].value == selectedValue)
            {
                ddlHours.selectedIndex = i;
                break;
            }
        }
    }
}

function bindDdlMinutes(calendarId, ddlHoursId, ddlMinutesId, minDayMinHour, minDayMinMinute, maxDayMaxHour, maxDayMaxMinute)
{
    var calendar = CalendarPopup_FindCalendar(calendarId);
    var calendarDate = calendar.GetDate();
    var calendarLowerBoundDate = calendar.LowerBoundDate;
    var calendarUpperBoundDate = calendar.UpperBoundDate;
    
    var ddlHours = document.getElementById(ddlHoursId);
    var ddlMinutes = document.getElementById(ddlMinutesId);
    
    var minMinute = 0;
    var maxMinute = 45;
    
    var selectedValue = null;
    
    if(ddlMinutes.length > 0)
    {
        selectedValue = ddlMinutes.options[ddlMinutes.selectedIndex].value;
    }
    
    if (calendarDate.getFullYear() == calendarLowerBoundDate.getFullYear() && 
        calendarDate.getMonth() == calendarLowerBoundDate.getMonth() &&
        calendarDate.getDate() == calendarLowerBoundDate.getDate() &&
        Number(ddlHours.options[ddlHours.selectedIndex].value) == Number(minDayMinHour))
    {
        minMinute = Number(minDayMinMinute);
    }
    if (calendarDate.getFullYear() == calendarUpperBoundDate.getFullYear() && 
        calendarDate.getMonth() == calendarUpperBoundDate.getMonth() &&
        calendarDate.getDate() == calendarUpperBoundDate.getDate() &&
        Number(ddlHours.options[ddlHours.selectedIndex].value) == Number(maxDayMaxHour))
    {
        maxMinute = Number(maxDayMaxMinute);
    }
    
    var ddlMinutesLength = ddlMinutes.length;
    
    for(var i = Number(ddlMinutesLength - 1); i >= 0; i--)
    {
        ddlMinutes.remove(i);
    }
    
    for(var i = Number(minMinute); i <= Number(maxMinute); i += 15)
    {
        var h;
        if(i == 0)
        {
            h = '00';
        }
        else
        {
            h = i;
        }
        insertOption(i, h, ddlMinutes.id);
    }
    
    ddlMinutes.selectedIndex = 0;
    if(selectedValue != null)
    {
        for(var i = 0; i < ddlMinutes.length; i++)
        {
            if(ddlMinutes.options[i].value == selectedValue)
            {
                ddlMinutes.selectedIndex = i;
                break;
            }
        }
    }
}

function getDayName(dayIndex)
{
    if(dayIndex == 0)
    {
        return 'Sunday';
    }
    else if(dayIndex == 1)
    {
        return 'Monday';
    }
    else if(dayIndex == 2)
    {
        return 'Tuesday';
    }
    else if(dayIndex == 3)
    {
        return 'Wednesday';
    }
    else if(dayIndex == 4)
    {
        return 'Thursday';
    }
    else if(dayIndex == 5)
    {
        return 'Friday';
    }
    else if(dayIndex == 6)
    {
        return 'Saturday';
    }
    return '';
}
